<?php
// $Header: /cvsroot/tsheet/timesheet.php/daily.php,v 1.7 2005/05/10 11:42:53 vexil Exp $

// Authenticate
require("class.AuthenticationManager.php");
require("class.CommandMenu.php");
if (!$authenticationManager->isLoggedIn()) 
{
	Header("Location: login.php?redirect=$_SERVER[PHP_SELF]");
	exit;
}

// Connect to database.
$dbh         = dbConnect();
$contextUser = strtolower($_SESSION['contextUser']);

if (empty($contextUser))
{
	errorPage("Could not determine the context user");
}
//define the command menu
include("timesheet_menu.inc");

//check that the client id is valid
if ($client_id == 0)
	$client_id = getFirstClient();

//check that project id is valid
if ($proj_id == 0)
	$task_id = 0;
	
//calculate tomorrow and yesterday for "prev" & "next" buttons
$yesterday = mktime(0,0,0,$month,$day,$year) - 24*60*60;
$tomorrow = mktime(0,0,0,$month,$day,$year) + 24*60*60;

function getDailyTimes($month, $day, $year, $id, $proj_id) {
	include("table_names.inc");
	list($qhq, $numq) = dbQuery("select timeformat from $CONFIG_TABLE where config_set_id = '1'");
	$configData = dbResult($qhq);
		
	$query = "select date_format(start_time,'%d') as day_of_month, trans_num, ";
	
	if ($configData["timeformat"] == "12")
		$query .= "date_format(end_time, '%l:%i%p') as endd, date_format(start_time, '%l:%i%p') as start, ";
	else
		$query .= "date_format(end_time, '%k:%i') as endd, date_format(start_time, '%k:%i') as start, ";
		$query .= "unix_timestamp(end_time) - unix_timestamp(start_time) as diff_sec, " .
							"unix_timestamp(start_time) as start_time, " .
							"unix_timestamp(end_time) as end_time, " .
							"end_time as end_time_str, " .
							"start_time as start_time_str, ".
							"$PROJECT_TABLE.title as project_title, " .
							"$CLIENT_TABLE.organisation as client, " .
							"$TASK_TABLE.name, $TIMES_TABLE.proj_id, $TIMES_TABLE.task_id " .
							"FROM $TIMES_TABLE, $TASK_TABLE, $PROJECT_TABLE, $CLIENT_TABLE " .
							"WHERE ($TASK_TABLE.source_project = $PROJECT_TABLE.title OR $TASK_TABLE.target_project = $PROJECT_TABLE.title) AND ($CLIENT_TABLE.client_id = $PROJECT_TABLE.client_id) AND " .
							"uid='$id' AND ";
	
	$query .= "$TASK_TABLE.task_id = $TIMES_TABLE.task_id AND " .
						"((start_time >= '$year-$month-$day 00:00:00' AND start_time <= '$year-$month-$day 23:59:59') " .
						" OR (end_time >= '$year-$month-$day 00:00:00' AND end_time <= '$year-$month-$day 23:59:59') " .
						" OR (start_time < '$year-$month-$day 00:00:00' AND end_time > '$year-$month-$day 23:59:59')) " .
						" order by day_of_month, start_time";

	list($my_qh, $num) = dbQuery($query);
		return array($num, $my_qh);
}


//include date input classes
include "form_input.inc";
list($num, $qh) = getDailyTimes($month, $day, $year, $contextUser, $proj_id);

?>
<html>
    <head>
        <title>Update timesheet for <?php echo $contextUser; ?></title>
        <?php include("header.inc");
        include("client_proj_task_javascript.inc");
        ?>
        <script language="Javascript">
        	function delete_entry(transNum) {
        				if (confirm('Are you sure you want to delete this time entry?'))
        					location.href = 'delete.php?client_id=<?php echo $client_id; ?>&proj_id=<?php echo $proj_id; ?>&task_id=<?php echo $task_id; ?>&trans_num=' + transNum;
        	}
        
        </script>
    </HEAD>
    
    <BODY <?php include ("body.inc"); ?> onload="doOnLoad();">
        <?php include ("banner.inc");
        ?>
        
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
        	<tr>
        		<td width="100%" class="face_padding_cell">
        				<table width="100%" border="0">
        					<tr>
        						<td align="left" nowrap class="outer_table_heading" nowrap>
        							Daily Timesheet
        						</td>
        						<td align="left" nowrap class="outer_table_heading">
        							<?php echo strftime("%A %B %d, %Y", mktime(0,0,0,$month, $day, $year)); ?>
        						</td>
        						<td align="right" nowrap>
        							<a href="<?php echo $_SERVER["PHP_SELF"]; ?>?month=<?php echo date("m",$yesterday); ?>&year=<?php echo date("Y",$yesterday); ?>&day=<?php echo date("d",$yesterday); ?>&proj_id=<?php echo $proj_id; ?>" class="outer_table_action">Prev</a>
        							<a href="<?php echo $_SERVER["PHP_SELF"]; ?>?month=<?php echo date("m",$tomorrow); ?>&year=<?php echo date("Y",$tomorrow); ?>&day=<?php echo date("d",$tomorrow); ?>&proj_id=<?php echo $proj_id; ?>" class="outer_table_action">Next</a>
        						</td>
        					</tr>
        				</table> 
        	<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="outer_table">
        		<tr>
        			<td>			
        				<table width="100%" border="0" cellpadding="0" cellspacing="0" class="table_body">
        					<tr class="inner_table_head">
        						<td class="inner_table_column_heading" align="center">Client</td>
								<td class="inner_table_column_heading" align="center">Project</td>
        						<td class="inner_table_column_heading" align="center">Task</td>
        						<td class="inner_table_column_heading" align="center">Duration</td>
        						<td class="inner_table_column_heading" align="center"><i>Actions</i></td>
        					</tr>
        <?php if ($num == 0) {
        	print "	<tr>\n";
        	print "		<td class=\"calendar_cell_middle\"><i>No days recorded.</i></td>\n";
        	print "		<td class=\"calendar_cell_middle\">&nbsp;</td>\n";
        	print "		<td class=\"calendar_cell_middle\">&nbsp;</td>\n";
        	print "		<td class=\"calendar_cell_middle\">&nbsp;</td>\n";
        	print "		<td class=\"calendar_cell_middle\">&nbsp;</td>\n";
        	print "		<td class=\"calendar_cell_disabled_right\">&nbsp;</td>\n";
        	print "	</tr>\n";
        	print "</table>\n";
        }
        else {
        	$last_task_id    = -1;
        	$today_total_sec = 0;
        	$total_diff_sec  = 0;
        	
        	while ($data = dbResult($qh)) {
        		//Due to an inconsistency with mysql and php with converting to unix timestamp from the string, 
        		//we are going to use php's strtotime to make the timestamp from the string.
        		//the problem has something to do with timezones.
        		$data["start_time"] = strtotime($data["start_time_str"]);
        		$data["end_time"]   = strtotime($data["end_time_str"]);
        
        		//get the project title and task name
        		$projectTitle = stripslashes($data["project_title"]);
        		$taskName     = stripslashes($data["name"]);
        		$client     = stripslashes($data["client"]);

				
        		//start printing details of the task
        		print "<tr>\n";
        		//print "<td class=\"calendar_cell_middle\"><a href=\"javascript:void(0)\" onclick=\"javascript:window.open('proj_info.php?proj_id=$data[proj_id]','Project Info','location=0,directories=no,status=no,scrollbar=yes,menubar=no,resizable=1,width=500,height=200')\">$projectTitle</a></td>\n";
        		//print "<td class=\"calendar_cell_middle\"><a href=\"javascript:void(0)\" onclick=\"javascript:window.open('task_info.php?task_id=$data[task_id]','Task Info','location=0,directories=no,status=no,scrollbar=yes,menubar=no,resizable=1,width=300,height=150')\">$taskName</a></td>\n";
        		print "<td class=\"calendar_cell_middle\">$client</td>\n";
				print "<td class=\"calendar_cell_middle\">$projectTitle</td>\n";
				print "<td class=\"calendar_cell_middle\">$taskName</td>\n";
        		
        		
				if ($data["diff_sec"] > 0) {
        			//if both start and end time are not today
        			if ($data["start_time"] < mktime(0,0,0,$month,$day,$year) && $data["end_time"] > mktime(23,59,59,$month,$day,$year))
        			{
        				$today_diff_sec = 24*60*60; //all day - no one should work this hard!
        
        				echo "<td class=\"calendar_cell_middle\" align=\"right\">" . formatSeconds($today_diff_sec). "<font color=\"#909090\"><i> of " . 
        						 		formatSeconds($data["diff_sec"]) . "</i></font></TD>\n";
        
        				
        			}		
        			//if end time is not today
        			elseif ($data["end_time"] > mktime(23,59,59,$month,$day,$year)) {
        				$today_diff_sec = mktime(0,0,0, $month,$day,$year) + 24*60*60 - $data["start_time"];
        			
        				echo "<td class=\"calendar_cell_middle\" align=\"right\">" . $data["start"] . "</td>" .
        						 
        						 "<td class=\"calendar_cell_middle\" align=\"right\">" . formatSeconds($today_diff_sec). "<font color=\"#909090\"><i> of " . formatSeconds($data["diff_sec"]) . "</i></font></td>\n";
        			}
        			//elseif start time is not today
        			elseif ($data["start_time"] < mktime(0,0,0,$month,$day,$year)) {
        				$today_diff_sec = $data["end_time"] - mktime(0,0,0, $month,$day,$year); 
        			
        				echo  "<td class=\"calendar_cell_middle\" align=\"right\">" . formatSeconds($today_diff_sec). "<font color=\"#909090\"><i> of " . 
        						 		formatSeconds($data["diff_sec"]) . "</i></font></td>\n";
        			}
        			else {
        				$today_diff_sec = $data["diff_sec"] / 60 / 60 / 8;
        				print "<td class=\"calendar_cell_middle\" align=\"right\">$today_diff_sec day</td>\n";
        			}
        
        			print "<td class=\"calendar_cell_disabled_right\" align=\"right\">\n";
        			//print "	<a href=\"edit.php?client_id=$client_id&proj_id=$proj_id&task_id=$task_id&trans_num=$data[trans_num]&year=$year&month=$month&day=$day\" class=\"action_link\">Details</a>,&nbsp;\n";
        			//print "	<a href=\"delete.php?client_id=$client_id&proj_id=$proj_id&task_id=$task_id&trans_num=$data[trans_num]\" class=\"action_link\">Delete</a>\n";
        			print "	<a href=\"javascript:delete_entry($data[trans_num]);\" class=\"action_link\">Delete</a>\n";
        			print "</td>";
        			
        			//add to todays total
        			$today_total_sec += $today_diff_sec;
        		}
        		else {			
        			print "<td class=\"calendar_cell_middle\" align=\"right\">$data[start]</td>\n";
        			print "<td class=\"calendar_cell_middle\" align=\"right\">&nbsp;</td>\n";
        			print "<td class=\"calendar_cell_middle\" align=\"right\">&nbsp;</td>\n";			
        			print "<td class=\"calendar_cell_disabled_right\" align=\"right\">\n";
        			print "	<a href=\"javascript:delete_entry($data[trans_num]);\" class=\"action_link\">Delete</a>\n";
        			print "</td>";
        		}
        		
        		print "</tr>";
        		
        	}
        	print "<tr>\n";
        	print "	<td class=\"calendar_totals_line_weekly_right\" colspan=\"5\" align=\"right\">";
        	print " Daily Total: <span class=\"calendar_total_value_weekly\"> $today_total_sec day </span></td>\n";
        	print "</tr>\n";
        	print "</table>";
        } 
        ?>
        	        		</td>		
        	        	</tr>
        	        </table>
        		</td>
        	</tr>
        </table>
        
        <!--table width="436" align="center" border="0" cellspacing="0" cellpadding="0">
        	<tr>
        		<td width="100%" class="face_padding_cell">
        			<table width="100%" border="0">
        				<tr>
        					<td align="left" nowrap class="outer_table_heading" nowrap>
        						Clock On / Off
        					</td>
        				</tr>
        			</table> 
        	        <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="outer_table">
                    
        	        	<form action="action.php" method="post" name="addForm" id="theForm">
        	        	<input type="hidden" name="destination" value="daily">
        	        	<input type="hidden" name="year"        value="<?php echo $year; ?>">
        	        	<input type="hidden" name="month"       value="<?php echo $month; ?>">
        	        	<input type="hidden" name="day"         value="<?php echo $day; ?>">
        	        	<input type="hidden" id="client_id"     value="<?php echo $client_id; ?>" name="client_id" >
        	        	<input type="hidden" id="proj_id"       value="<?php echo $proj_id; ?>"   name="proj_id"   >
        	        	<input type="hidden" id="task_id"       value="<?php echo $task_id; ?>"   name="task_id"   >																										
        	        	<input type="hidden" name="origin"      value="<?php echo $_SERVER["PHP_SELF"]; ?>">
        	        	<input type="hidden" name="destination" value="<?php echo $_SERVER["PHP_SELF"]; ?>">										
                    
        	        	<tr>
        	        		<td>				
        	        			<table width="100%" border="0" cellpadding="1" cellspacing="2" class="table_body">			
        	        				<tr>
        	        					<td>
        	        						<table width="100%" border="0">
        	        							<tr>
        	        								<td align="left" width="100%" nowrap>
        	        										<table width="100%" border="0" cellspacing="0" cellpadding="0">
        	        											<tr>
        	        												<td><table width="50"><tr><td>Client:</td></tr></table></td>
        	        												<td width="100%">
        	        													<select id="clientSelect" name="clientSelect" onChange="onChangeClientSelect();" style="width: 100%;" />
        	        												</td>
        	        											</tr>
        	        										</table>
        	        								</td>									
        	        							</tr>																									
        	        							<tr>
        	        								<td align="left" width="100%" nowrap>
        	        										<table width="100%" border="0" cellspacing="0" cellpadding="0">
        	        											<tr>
        	        												<td><table width="50"><tr><td>Project:</td></tr></table></td>
        	        												<td width="100%">
        	        													<select id="projectSelect" name="projectSelect" onChange="onChangeProjectSelect();" style="width: 100%;" />
        	        												</td>
        	        											</tr>
        	        										</table>
        	        								</td>									
        	        							</tr>																		
        	        							<tr>
        	        								<td align="left" width="100%">
        	        									<table width="100%" border="0" cellspacing="0" cellpadding="0">
        	        										<tr>
        	        											<td>
        	        											    <table width="50">
        	        												    <tr>
        	        													    <td>Task:</td>
        	        													</tr>
        	        												</table>
        	        											</td>
        	        											<td width="100%">
        	        												<select id="taskSelect" name="taskSelect" onChange="onChangeTaskSelect();" style="width: 100%;" />
        	        											</td>
        	        										</tr>
        	        									</table>
        	        								</td>									
        	        							</tr>																										
        	        							<tr>
        	        								<td>
        	        									<table width="100%" border="0">
        	        										<tr>								
                    	            	    					<td align="center">
                    	            	    						<table width="400" border="0" class="clock_on_box">
                    	            	    							<tr>
                    	            	    								<td>
                    	            	    									<img src="images/clock-green-sml.gif" border="0">
                    	            	    								</td>
                                                                            <td align="left" class="clock_on_text">
                    	            	    									Time available for this task:
                    	            	    								</td>																			
                    	            	    								<td valign="middle">
        	        															<select name="dayInput" id="dayInput" size="1">
                                                                                    <option>0.25</option>
                                                                                    <option>0.5</option>
                                                                                    <option>0.75</option>
                                                                                    <option selected value="">1
                                                                                </select>
                    	            	    								</td>
        	        														
        	        														<td align="left" class="clock_on_text">
                    	            	    									day
                    	            	    								</td>
                    	            	    							</tr>								
                    	            	    						</table>
                    	            	    					</td>
                    	            	    				</tr>
        	        										<tr>
        	        											<td align="center">
        	        												<input type="button" value="Validate" name="submitButton" id="submitButton" onClick="onSubmit();">
        	        											</td>
        	        										</tr>																												
        	        									</table>
        	        								</td>									
        	        							</tr>
        	        						</table>
        	        					</td>					
        	        				</tr>							
        	        			</table>
        	        		</td>
        	        	</tr>
        	        	</form>	
        	        </table>	
        		</td>			
        	</tr>
        </table-->
        	
        <?php include ("footer.inc");
        ?>
    </BODY>
</HTML>
